package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private RequestMappingHandlerMapping rmhm;

    @Autowired
    private PermissionMapper mapper;

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void save(Permission record) {
        mapper.insert(record);
    }

    /*public Permission get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }*/

    public List<Permission> list() {
        return mapper.selectAll();
    }

    public void update(Permission record) {
        mapper.updateByPrimaryKey(record);
    }

    //分页
    public PageResult query(QueryObject qo){
        int count = mapper.queryForCount(qo);
        List<Permission> list = mapper.queryForList(qo);
        if (count==0){
            return new PageResult();
        }
        return new PageResult(count,list);
    }

    //加载权限
    public  void load(){
        //排除重复权限
        List<Permission> permissions = mapper.selectAll();
        Set<String> set = new HashSet<>();
        for (Permission p : permissions){
            set.add(p.getResource());
        }
        //通过请求的映射处理映射处理器获取所有的处理方法
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();
        //遍历方法对象,判断该方法上是否贴有RequiresPermissions注解
        for (HandlerMethod hm: values){
            RequiresPermissions annotation = hm.getMethodAnnotation(RequiresPermissions.class);
            if (annotation==null){
                continue;
            }
            //shiro权限注解value是数组,返回就是数组
            //@RequiresPermissions(value={"employee:index", "员工的名称"}
            String resource = annotation.value()[0];
            if (set.contains(resource)){
                continue;
            }
            //获取权限名称
            PermissionName permissionName = hm.getMethodAnnotation(PermissionName.class);
            if (permissionName==null){
                continue;
            }
            String name = permissionName.value();
            //将权限名称+权限表达式封装成权限对象
            Permission permission = new Permission();
            permission.setName(name);
            permission.setResource(resource);
            mapper.insert(permission);
        }
    }

    public List<String> queryPermissionResourceByEmpId(Long id) {
        return mapper.queryPermissionResourceByEmpId(id);
    }

    public List<Permission> queryPermissionByRoleId(Long roleId){
        return mapper.queryPermissionByRoleId(roleId);
    }

}
